#include "datamodel.h"

DataModel::DataModel(QObject *parent)
    : QAbstractListModel(parent)
{
    // 添加一些示例联系人数据
    m_items.append(ContactItem("张三", "13800138000"));
    m_items.append(ContactItem("李四", "13900139000"));
    m_items.append(ContactItem("王五", "13700137000"));
    
    // 保存原始数据
    m_originalItems = m_items;
}

int DataModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_items.count();
}

QVariant DataModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= m_items.count())
        return QVariant();

    const ContactItem &item = m_items.at(index.row());

    switch (role) {
    case NameRole:
        return item.name();
    case PhoneRole:
        return item.phone();
    case FirstLetterRole:
        return item.firstLetter();
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> DataModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[PhoneRole] = "phone";
    roles[FirstLetterRole] = "firstLetter";
    return roles;
}

bool DataModel::addContact(const QString &name, const QString &phone)
{
    if (name.isEmpty() || phone.isEmpty())
        return false;

    beginInsertRows(QModelIndex(), m_items.count(), m_items.count());
    m_items.append(ContactItem(name, phone));
    m_originalItems = m_items;  // 更新原始数据
    endInsertRows();
    return true;
}

bool DataModel::removeContact(int index)
{
    if (index < 0 || index >= m_items.count())
        return false;

    // 获取要删除的联系人信息
    const ContactItem &itemToRemove = m_items.at(index);
    
    // 从当前显示列表中删除
    beginRemoveRows(QModelIndex(), index, index);
    m_items.removeAt(index);
    endRemoveRows();

    // 从原始数据中找到并删除对应项
    for (int i = 0; i < m_originalItems.count(); ++i) {
        if (m_originalItems.at(i).name() == itemToRemove.name() &&
            m_originalItems.at(i).phone() == itemToRemove.phone()) {
            m_originalItems.removeAt(i);
            break;
        }
    }
    
    return true;
}

QVariantList DataModel::searchContacts(const QString &keyword)
{
    if (keyword.isEmpty()) {
        beginResetModel();
        m_items = m_originalItems;
        endResetModel();
        return QVariantList();
    }

    QVariantList results;
    beginResetModel();
    m_items.clear();
    
    for (const ContactItem &item : m_originalItems) {
        if (item.name().contains(keyword, Qt::CaseInsensitive) ||
            item.phone().contains(keyword, Qt::CaseInsensitive)) {
            m_items.append(item);
        }
    }
    endResetModel();
    return results;
}

void DataModel::clearSearch()
{
    beginResetModel();
    m_items = m_originalItems;
    endResetModel();
}

bool DataModel::editContact(int index, const QString &name, const QString &phone)
{
    if (index < 0 || index >= m_items.count() || name.isEmpty() || phone.isEmpty())
        return false;

    // 获取要修改的联系人信息
    const ContactItem &itemToEdit = m_items.at(index);
    
    // 创建新的联系人项
    ContactItem newItem(name, phone);
    
    // 更新当前显示列表中的项
    m_items[index] = newItem;
    emit dataChanged(createIndex(index, 0), createIndex(index, 0));

    // 在原始数据中找到并更新对应项
    for (int i = 0; i < m_originalItems.count(); ++i) {
        if (m_originalItems.at(i).name() == itemToEdit.name() &&
            m_originalItems.at(i).phone() == itemToEdit.phone()) {
            m_originalItems[i] = newItem;
            break;
        }
    }
    
    return true;
} 